﻿

Imports Client.PowerShell




Public Class PowerShellForm



    Private WithEvents _Run As IRun

    Private _RunSync As IRun

    Private _RunASync As IRun



    Public Sub New()



        ' This call is required by the designer.

        InitializeComponent()



        ' Add any initialization after the InitializeComponent() call.

        _RunSync = New RunSync

        _RunASync = New RunASync

    End Sub



    Private Sub Clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click

        txtResult.Text = ""

    End Sub



    Private Sub Run_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRun.Click

        Run(_RunSync)

    End Sub



    Private Sub Run(ByVal RunType As IRun)

        btnRun.Enabled = False

        btnRunAsync.Enabled = False

        _Run = RunType

        _Run.Run(Me.txtCommand.Text)

    End Sub



    Private Delegate Sub RunCompleted(ByVal Result As String)



    Private Sub _Run_OutputChanged(ByVal Output As String) Handles _Run.OutputChanged

        If Me.InvokeRequired Then

            Me.Invoke(New RunCompleted(AddressOf _Run_OutputChanged), New Object() {Output})

        Else

            Me.txtResult.AppendText(Output)

        End If

    End Sub



    Private Sub btnForLoop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnForLoop.Click

        Me.txtCommand.AppendText("for($x = 0; $x -le 10; $x++)" & Environment.NewLine)

        Me.txtCommand.AppendText("{" & Environment.NewLine)

        Me.txtCommand.AppendText("Write ""Child Task $x"" ;" & Environment.NewLine)

        Me.txtCommand.AppendText("Sleep -Milliseconds 300;" & Environment.NewLine)

        Me.txtCommand.AppendText("}" & Environment.NewLine)

    End Sub



    Private Delegate Sub RunFinishedDelegate()



    Private Sub _Run_RunFinished() Handles _Run.RunFinished

        If Me.InvokeRequired Then

            Me.Invoke(New RunFinishedDelegate(AddressOf _Run_RunFinished), Nothing)

        Else

            Me.btnRun.Enabled = True

            Me.btnRunAsync.Enabled = True

        End If

    End Sub



    Private Sub btnRunAsync_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRunAsync.Click

        Run(_RunASync)

    End Sub

End Class
